$(document).ready(function() {
	
	$('input[type="text"]').click(function(){
		var value = $(this).val();
		if (value == '请在此输入车型名称') {
			$(this).val('');
		}
	});
    $("#s_brand").change(function(){
        var b_id = $("#s_brand").val();
        $('#s_model').empty();
        $('#s_model').fadeIn();
        $('#s_model').append("<option value='0'>-请选择车系-</option>");
		$.post("/ajax/getmodel",{b_id:b_id},function(result){                      
        	$('#s_model').append(result);  
		});
        
    });
    $("#s_model").change(function(){
        var m_id = $("#s_model").val();
        $('#s_carname').empty();
        $('#s_carname').fadeIn();
        $('#s_write').show();
        $('#model_text').hide();
        $("#is_write").val("0");
        $('#s_carname').fadeIn();
        $('#s_carname').append("<option value='0'>-请选择车型-</option>");
		$.post("/ajax/getcarname",{m_id:m_id},function(result){                      
        	$('#s_carname').append(result);  
		});
        
    });
    $("#write_model").click(function(){
    	$('#s_carname').toggle();
    	$('#model_text').toggle();
    	var now = $(this).html();
    	if (now == "切换为手动输入车型") {
    		$(this).html("切换为选择车型");
    		$("#is_write").val("1");
		}else{
			$(this).html("切换为手动输入车型");
			$("#is_write").val("0");
		}
		$("#c_brand").removeClass("check_right");
    });
    
    $("#s_province").change(function(){
        var id = $("#s_province").val();
        $('#s_city').empty();
        $('#s_city').fadeIn();
        $('#s_city').append("<option value='0'>-请选择城市-</option>");

		$.post("/ajax/getcity",{id:id},function(result){                      
        	$('#s_city').append(result);  
		});
        
    });
    
	$('select[name="check_year"]').change(function(){
		var value = $(this).val();
		if (value == '0' || value == '-1') {
			$('select[name="check_month"]').fadeOut();
		}else{
			$('select[name="check_month"]').fadeIn();
		}
	});
	$('select[name="safe_year"]').change(function(){
		var value = $(this).val();
		if (value == '0' || value == '-1') {
			$('select[name="safe_month"]').fadeOut();
		}else{
			$('select[name="safe_month"]').fadeIn();
		}
	});
	
	
	//检查我要卖车 输入
	var now_check = false;
	
	$("#sale_sub").click(function(){
		if (check_brand()) {
			if(check_model()){
				check_carname();
			}
		}
		check_color();
		if (check_province()) {
			check_city();
		}
		check_kilometer();
		if (check_car_year()) {
			check_car_month();
		}
		if (check_check_year()) {
			check_check_month();
		}
		if (check_safe_year()) {
			check_safe_month();
		}
		check_note();
		check_price();
		check_contact_user();
		check_contact_tel();
		
		if (now_check == true) {
			$("form").submit();
		}else{
			$("html,body").animate({scrollTop: $('.warning').offset().top - 50}, 800);
			return false;
		}

	});
	$("#s_brand").change(function(){
		check_brand();
		$("#c_brand").removeClass("check_right");
	});
	$("#s_model").change(function(){
		check_model();
		$("#c_brand").removeClass("check_right");
	});
	$("#s_carname").change(function(){
		check_carname();
	});
	$("#model_text").change(function(){
		check_model();
	});
	$('#s_color').change(function(){
		check_color();
	});
	$('#s_province').change(function(){
		check_province();
		$("#c_province").removeClass("check_right");
	});
	$('#s_city').change(function(){
		check_city();
	});
	
	$('input[name="kilometer"]').change(function(){
		check_kilometer();
	});
	
	$('select[name="car_year"]').change(function(){
		check_car_year();
		$("#car_year").removeClass("check_right");
	});
	$('select[name="car_month"]').change(function(){
		check_car_month();
	});
	
	$('select[name="check_year"]').change(function(){
		check_check_year();
		$("#check_year").removeClass("check_right");
	});
	$('select[name="check_month"]').change(function(){
		check_check_month();
	});
	$('select[name="safe_year"]').change(function(){
		check_safe_year();
		$("#safe_year").removeClass("check_right");
	});
	$('select[name="safe_month"]').change(function(){
		check_safe_month();
	});
	$('textarea[name="note"]').change(function(){
		check_note();
	});
	$('input[name="price"]').change(function(){
		check_price();
	});

	$('input[name="contact_user"]').change(function(){
		check_contact_user();
	});
	$('input[name="contact_tel"]').change(function(){
		check_contact_tel();
	});

	
	
	//卖车页面 返回
	$('#stop_sale').confirm({
		'message' : '确定要离开吗? 您填写的信息将会丢失！？'
	});
	
	//MEMBER 注册验证
	$('input[name="username"]').blur(function(){
		check_M_all("username");
	});
	$('input[name="password"]').blur(function(){
		check_M_all("password");
	});
	$('input[name="passwordagain"]').blur(function(){
		check_M_passwordagain();
	});
	$('input[name="email"]').blur(function(){
		check_M_all("email");
	});
	$('input[name="captcha"]').blur(function(){
		check_M_all("captcha");
	});
	$('input[name="coname"]').blur(function(){
		check_M_all("coname");
	});
	$('input[name="address"]').blur(function(){
		check_M_all("address");
	});
	$('input[name="realname"]').blur(function(){
		check_M_Notmust("realname");
	});
	$('input[name="idno"]').blur(function(){
		check_M_Notmust("idno");
	});
	$('input[name="phone"]').blur(function(){
		check_M_Notmust("phone");
	});
	
	//LOGIN 验证
	$('input[name="loginaccount"]').blur(function(){
		check_M_all("loginaccount");
		var value = $('input[name="loginaccount"]').val();
		if (value != ""){
			$.post("/ajax/isprotectlogin",{value:value},function(result){   
				$('#ajax').html(result);
			});
		}
	});
	
	$('input[name="loginpassword"]').blur(function(){
		check_M_loginpassword();
	});
	
	//REG 提交 
	var reg_check = false;
	$("#register_sub").click(function(){
		var type = $('#type').val();
		
		if (type == 2){
			//商家
			check_M_all("coname");
			check_M_all("address");
			check_M_Notmust("realname");
			check_M_Notmust("idno");
			check_M_Notmust("phone");
		}
		check_M_all("username");
		check_M_all("password");
		check_M_passwordagain();
		check_M_all("email");
		check_M_all("captcha");
		check_agreement();
		if (reg_check == true){
			$("form").submit();
		}else{
			$("html,body").animate({scrollTop: $('.icon-remove-sign').offset().top - 50}, 800);
			return false;
		}
	});
	
	//LOGIN 提交
	$("#login_sub").click(function(){
		var iscaptcha = $('#iscaptcha').val();
		if (iscaptcha == 1) {
			check_M_all("captcha");
		}
		
		var username = $('input[name=loginaccount]').val();
		var password = $('input[name=loginpassword]').val();
		var captcha = $('input[name=captcha]').val();
		var autologin = 0;
	    $("input[name='autologin']").each(function(){
	        if(this.checked == true) {
	        	autologin = 1;
	        }
	    });
		var ref = $('input[name=ref]').val();
		$.post("/ajax/dologin",{username:username, password:password, captcha:captcha, autologin:autologin, ref:ref},function(result){   
			$('#ajax').html(result);
		});

		return false;
	});
	
	
	
	//找回密码
	$("button[id=forgot_sub]").click(function(){
		var step = $('input[name=step]').val();
		var account = $('input[name=loginaccount]').val();
		var email = $('input[name=forgot_email]').val();
		var answer = $('input[name=answer]').val();
		var captcha = $('input[name=captcha]').val();
		var mode = $('input[name=mode]').val();
		$.post("/ajax/doforgot",{step:step, account:account, email:email, answer:answer, captcha:captcha, mode:mode},function(result){   
			if (result == 1){
				$("form").submit();
			}else{
				errorMsg(result);
				//$('#ajax').html(result);
				return false;
			}
		});
		
	});
	
	//设置新密码
	$("#set_newpw").click(function(){
		var password = $('input[name=password]').val();
		var passwordagain = $('input[name=passwordagain]').val();
		$.post("/ajax/setnewpw",{password:password, passwordagain:passwordagain},function(result){   
			$('#ajax').html(result);
			return false;
		});
	});
	
	
});


//删除上传图片
function del_upload(id){
	$('#' + id).html("");
	$('#' + id).attr('id','');
	//总数量 -1
	delPicNum();
	//var session_id = $('#session_id').val();
	/*
	$.post("/ajax/removeimg",{id:id, session_id:session_id},function(result){  
		alert(result);
	});*/
}

//上传图片设封面
function add_front(id){
	$('div').removeClass("Front_vis");
	$("#Pic" + id).addClass("Front_vis");
	var front_id = $(".Front_vis input").val();
	$('#front').val(front_id);
	//<div id="divShowTop1_1" class="pic_ico1" style="">封</div>
}

//登陆时显示验证码
function iscaptcha(str){
	if (str == 1){
		$(".login-captcha").show();
		$("#iscaptcha").val('1');
	}else{
		$(".login-captcha").hide();
		$("#iscaptcha").val('0');
	}
	
}


//check_M 为 MEMBER CHECK 未说明为 SALE CHECK
function check_M_all(id){
	var value = $('input[name="'+id+'"]').val();
	var placeholder = $('input[name="'+id+'"]').attr('placeholder');
	if (value == "" || value == placeholder) {
		return showPopError(id, '此项不能为空');
	}else{
		$.post("/ajax/get"+id+"check",{value:value},function(result){   
	    	if (result == 1) {
	    		return hidePopError(id);
			}else{
				return showPopError(id, result);
			}
		});
	}
}

//NOT MUST CHECK NOT CHECK NULL
function check_M_Notmust(id){
	var value = $('input[name="'+id+'"]').val();
	var placeholder = $('input[name="'+id+'"]').attr('placeholder');
	if (value == "" || value == placeholder) {
		//return showPopError(id, '此项不能为空');
		return hidePopError(id);
	}else{
		$.post("/ajax/get"+id+"check",{value:value},function(result){   
	    	if (result == 1) {
	    		return hidePopError(id);
			}else{
				return showPopError(id, result);
			}
		});
	}
}



function check_M_passwordagain(){
	var pw = $('input[name="password"]').val();
	var pw2 = $('input[name="passwordagain"]').val();
	if ((pw2 == "") || (pw != pw2)){
		return showPopError("passwordagain", '两次输入的密码不一致');
	}else{
		return hidePopError('passwordagain');
	}
}



function check_M_loginpassword(){
	//var username = $('input[name=loginaccount]').val();
	var password = $('input[name=loginpassword]').val();
	if (password == "") {
		return showPopError('loginpassword', '密码不能为空');
	}else{
		return hidePopError('loginpassword');
	}
}

function check_agreement(){
	
    $("input[name='agreement']").each(function(){
        if(this.checked == true) {
        	return hidePopError('agreement');
        }else{
    		return showPopError("agreement", '您必须同意服务条款才能继续');
    	}
    });

}
/*
function rightPop(id){
	hidePopError(id);
    $('input[name="'+id+'"]').popover({
    	html: true,
    	placement: 'left',
    	content: '<span class="help-inline check_right">&nbsp;</span>',
    });
    $('input[name="'+id+'"]').popover('show');
    return true;
}*/

//销毁错误提示
function hidePopError(id){
	$("input[name='"+id+"']").popover('destroy');
	reg_check = true;
	return true;
}

//弹出错误
function showPopError(id, value){
	hidePopError(id);
    $('input[name="'+id+'"]').popover({
    	html: true,
    	placement: 'left',
    	content: "<i class='icon-remove-sign'></i> <font color='red'>" + value + "</font>",
    });
    $('input[name="'+id+'"]').popover('show');
    reg_check = false;
    return false;
}

//CHECK FUNCTION
function check_brand(){
	var value = $("#s_brand").val();
	
	if (value == 0) {
		return showError("c_brand", "请选择车辆品牌");
	}else{
		return hideError("c_brand");
	}
}
function check_model(){
	var value = $("#s_model").val();
	
	if (value == 0) {
		return showError("c_brand", "请选择车系");
	}else{
		return hideError("c_brand");
	}
}
function check_carname(){
	var value = $("#s_carname").val();
	var model_text = $("#model_text").val();
	var is_write = $('#is_write').val();
	if(is_write == 0){
		if (value == 0) {
			return showError("c_brand", "请在列表中选择车型");
		}else{
			return hideError("c_brand");
		}
	}else if(is_write == 1){
		if (model_text == "" || model_text == "请在此输入车型名称") {
			return showError("c_brand", "请在输入框中输入您的车型名称");
		}else{
			return hideError("c_brand");
		}
	}

}
function check_color(){
	var value = $('#s_color').val();
	if (value == 0) {
		return showError("c_color", "请选择车辆颜色");
	}else{
		return hideError("c_color");
	}
}
function check_province(){
	var value = $('#s_province').val();
	if (value == 0) {
		return showError("c_province", "请选择车辆所在省份");
	}else{
		return hideError("c_province");
	}
}
function check_city(){
	var value = $('#s_city').val();
	if (value == 0) {
		return showError("c_province", "请选择车辆所在城市");
	}else{
		return hideError("c_province");
	}
}
function check_kilometer(){
	var value = $('input[name="kilometer"]').val();
	if (value == 0) {
		return showError("c_kilometer", "请填写表显里程");
	}else{
		if(isNaN(value)){
			return showError("c_kilometer", "里程格式不正确,请填写数字");
		}else{
			return hideError("c_kilometer");
		}
	}
}
function check_car_year(){
	var value = $('select[name="car_year"]').val();
	if (value == 0) {
		return showError("car_year", "请选择上牌年份");
	}else{
		return hideError("car_year");
	}
}
function check_car_month(){
	var value = $('select[name="car_month"]').val();
	if (value == 0) {
		return showError("car_year", "请选择上牌月份");
	}else{
		return hideError("car_year");
	}
}
function check_check_year(){
	var value = $('select[name="check_year"]').val();
	if (value == 0) {
		return showError("check_year", "请选择年检有效期年份");
	}else{
		return hideError("check_year");
	}
}

function check_check_month(){
	var value = $('select[name="check_month"]').val();
	var year = $('select[name="check_year"]').val();
	if (value == 0 && year != -1) {
		return showError("check_year", "请选择年检有效期月份");
	}else{
		return hideError("check_year");
	}
}
function check_safe_year(){
	var value = $('select[name="safe_year"]').val();
	if (value == 0) {
		return showError("safe_year", "请选择保险有效期年份");
	}else{
		return hideError("safe_year");
	}
}
function check_safe_month(){
	var value = $('select[name="safe_month"]').val();
	var year = $('select[name="safe_year"]').val();
	if (value == 0 && year != -1) {
		return showError("safe_year", "请选择保险有效期月份");
	}else{
		return hideError("safe_year");
	}
}
function check_note(){
	var value = $('textarea[name="note"]').val();
	var placeholder = $('textarea[name="note"]').attr('placeholder');
	if (value == "" || value == placeholder) {
		return showError("c_note", "请填写自述内容");
	}else{
		return hideError("c_note");
	}
}

function check_price(){
	var value = $('input[name="price"]').val();
	if (value == "") {
		return showError("c_price", "请填写预售价格");
	}else{
		if(isNaN(value)){
			return showError("c_price", "价格格式不正确,请填写数字");
		}else{
			return hideError("c_price");
		}
	}
}

function check_contact_user(){
	var value = $('input[name="contact_user"]').val();
	var placeholder = $('input[name="contact_user"]').attr('placeholder');
	if (value == "" || value == placeholder) {
		return showError("c_contact_user", "请填写联系人姓名");
	}else{
		return hideError("c_contact_user");
	}
}
function check_contact_tel(){
	var value = $('input[name="contact_tel"]').val();
	if (value == "") {
		return showError("contact_tel", "请填写您的联系手机");
	}else{
		if(!(value.match(/^1[3|4|5|8][0-9]\d{4,8}$/))){
			return showError("contact_tel", "联系手机格式不正确,请修改");
		}else{
			return hideError("contact_tel");
		}
	}
}


//返回
function showError(id, value){
	$("#"+id).text(" * "+value);
	$("#"+id).removeClass("check_right");
	$("#"+id).addClass("warning");
	now_check = false;
	return false;
}

function hideError(id){

	$("#"+id).html("&nbsp;");
	$("#"+id).removeClass("warning");
	$("#"+id).addClass("check_right");
	now_check = true;
	return true;
}
